package com.mcflies.ui.drawable;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mcflies.commons.McfliesConstant;
import com.mcflies.commons.MenuMainAdapter;
import com.mcflies.ui.R;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

public class DrawableMainActivity extends Activity implements
		OnItemClickListener {
	private GridView gridView;
	private AssetManager assetManager;
	private String[] drawableMainMenuArray;
	private List<Map<String, Object>> drawableDataList = new ArrayList<Map<String, Object>>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu_main);
		initWidget();
		initData();

		gridView.setAdapter(new MenuMainAdapter(this, drawableDataList));
		gridView.setOnItemClickListener(this);
	}

	private void initData() {
		drawableMainMenuArray = getResources().getStringArray(
				R.array.drawableMainMenuArray);
		assetManager = getAssets();
		initMenuDataList(drawableMainMenuArray);
	}

	private void initWidget() {
		gridView = (GridView) findViewById(R.id.menuMainGridView);
	}

	@Override
	public void onItemClick(AdapterView<?> adapter, View view, int position,
			long id) {
		try {
			Map<String, Object> item = drawableDataList.get(position);
			String itemIntent = (String) item
					.get(McfliesConstant.MenuMainConstant.MENU_MAIN_INTENT);
			Intent intent = new Intent(DrawableMainActivity.this,
					Class.forName(itemIntent));
			startActivity(intent);
		} catch (Exception e) {
			Log.i(McfliesConstant.MCFLIES_EXCEPTION_INFO_FLAG, e.getMessage());
		}
	}

	private void initMenuDataList(String[] menuMainMenuArray) {
		for (int i = 0; i < menuMainMenuArray.length; i++) {
			String tempItemStr = menuMainMenuArray[i];
			String[] tempItemArray = tempItemStr.split("@");
			if (tempItemArray.length != 3) {
				continue;
			}

			Map<String, Object> tempMap = new HashMap<String, Object>();
			// 菜单标题
			String tempMenuDes = tempItemArray[0];
			tempMap.put(McfliesConstant.MenuMainConstant.MENU_MAIN_DES,
					tempMenuDes);

			// 菜单图片
			String tempMenuImageName = tempItemArray[1];
			if (!(tempMenuImageName.endsWith(".jpg")
					|| tempMenuImageName.endsWith(".png") || tempMenuImageName
						.endsWith(".ico"))) {
				tempMenuImageName = "menuMain_default.png";
			}
			InputStream is = null;
			try {
				is = assetManager
						.open("drawableMainMenuImage/" + tempMenuImageName);
				if (null == is) {
					is = assetManager
							.open("drawableMainMenuImage/menuMain_default.png");
				}
			} catch (Exception e) {
				Log.i(McfliesConstant.MCFLIES_EXCEPTION_INFO_FLAG,
						"DrawableMainActivity&&initMenuDataList()方法异常："
								+ e.getMessage());
			}

			Bitmap tempMenuImage = BitmapFactory.decodeStream(is);
			tempMap.put(McfliesConstant.MenuMainConstant.MENU_MAIN_IMAGE,
					tempMenuImage);

			// 点击菜单目标Activity
			String tempMenuIntent = tempItemArray[2];
			tempMap.put(McfliesConstant.MenuMainConstant.MENU_MAIN_INTENT,
					tempMenuIntent);
			drawableDataList.add(tempMap);
		}
	}
}
